@extends('admin.layout.base')
@section('title', '权限列表')
@section('pluginstyle')
<meta name="csrf-token" content="{{ csrf_token() }}" />
@endsection
@section('content')
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            管理员权限列表页面
        </h1>
        <ol class="breadcrumb">
            <li><a href="{{ url('/admin') }}"><i class="fa fa-dashboard"></i> 首页</a></li>
            <li class="active">权限管理</li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-header">
                        <div class="btn-group">
                            <button class="btn btn-success status" data-status="1">选中有效</button>
                            <button class="btn btn-warning status" data-status="0">选中无效</button>
                            <button class="btn btn-danger del" data-status="-1">选中删除</button>
                        </div>
                        <a class="btn btn-success" href="{{ url('/admin/rule/create') }}">
                            <i class="fa fa-plus"></i> 添加权限
                        </a>
                        <div class="box-tools">
                            <form action="{{ url('/admin/rule') }}" method="get">
                            <div class="input-group" style="width: 150px;">
                                <input type="text" name="name" value="{{ old('name') }}" class="form-control pull-right" placeholder="权限名称">
                                <div class="input-group-btn">
                                    <button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
                                </div>
                            </div>
                            </form>
                        </div>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body table-responsive no-padding">
                        <table class="table table-hover table-bordered">
                            <tr>
                                <th>
                                    <input type="checkbox" name="" id="checkall"/>全选
                                    #
                                </th>
                                <th>权限</th>
                                <th>名称</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            @foreach($rules as $rule)
                            <tr>
                                <td>
                                    <input type="checkbox" name="id[]" class="subcheck" value="{{ $rule->id }}"/>
                                    {{ $rule->id }}
                                </td>
                                <td>{{ $rule->name }}</td>
                                <td>{{ $rule->title }}</td>
                                <td>{{ $rule->status==1 ? '有效' : '无效' }}</td>
                                <td>
                                    <a href="{{ url("/admin/rule/{$rule->id}/edit") }}" class="btn btn-info">编辑</a>
                                </td>
                            </tr>
                            @endforeach
                        </table>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                        {!! $rules->render() !!}
                    </div>
                </div>
                <!-- /.box -->
            </div>
        </div>
    </section>
    <!-- /.content -->
</div>
@endsection
@section('pluginscript')
<!-- Slimscroll -->
<script src="{{ asset('/dist/admin/plugins/slimScroll/jquery.slimscroll.min.js') }}"></script>
<!-- FastClick -->
<script src="{{ asset('/dist/admin/plugins/fastclick/fastclick.js') }}"></script>
<script type="text/javascript">
    $(function(){
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        
        // 全选
        $('#checkall').click(function(){
            $('.subcheck').attr('checked', this.checked);
        });
        $('.subcheck').click(function(){
            if(this.checked == false){
                $("#checkall").attr('checked', false);
            }
        });
        
        // 修改权限有效性
        $('.status').click(function(){
            var url = '{{ url("/admin/rule/multi") }}';
            var status = $(this).attr('data-status');
            var id = [];
            $('.subcheck').each(function(i){
                if(this.checked !== false){
                    id[i] = $(this).val();
                }
            });
            
            $.post(url,{status:status,id:id}, function(msg){
                if(msg.status != '1'){
                    alert(msg.info);
                }
                location.reload();
            });
        });
        
        // 删除
        $('.del').click(function(){
            var url = "{{ url('/admin/rule/delete') }}";
            var result = confirm('确定要删除？');
            var id = [];
            $('.subcheck').each(function(i){
                if(this.checked !== false){
                    id[i] = $(this).val();
                }
            });
            
            if(result){
                $.ajax({
                    url: url,
                    type:'post',
                    data:{id:id},
                    success:function(msg){
                        if(msg.status == '1'){
                            location.reload();
                        } else {
                            alert(msg.info);
                        }
                    }
                });
            }
            return false;
        });
        
    })
</script>
@endsection
